import axios from 'axios'
import {onWSMessage} from "../js/websocket/websocket.js";





export let BASE_URL = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;

export let BASE_URL_SERVE = window.location.protocol + '//' + window.location.hostname ;
export let BASE_URL_WEBSOCKET_SERVE = 'ws://' + window.location.hostname + '/webSocket';//websocket的地址



/**
 * 这里封装了axios请求
 * 加入了全局request 和 response 的拦截,在request中加入了鉴权的token
 * 然后把响应全部泛型为FangResponse
 */
// let BASE_URL = window.location.protocol + '//' + window.location.hostname + ':10008';
    //    超市时间:30s
const TIMEOUT = 30000

class FARequest {
    instance

    constructor() {
        this.instance = axios.create({
            timeout: TIMEOUT,
            //前后端,默认json传输
            headers: {
                'Content-Type': 'application/json'
            },
        })

        //使用传进来的interceptors  针对所有实例的拦截
        this.instance.interceptors.request.use(
            (config) => {
                // @ts-ignore
                // config.headers['Authentication'] = tokenUtil.getToken()
                // console.log(config);
                // console.log('所有请求拦截');
                return config
            },
            (err) => {
                // console.log('所有请求错误拦截');
                return err
            }
        )
        this.instance.interceptors.response.use(
            (res) => {
                // console.log('所有响应拦截');
                return res
            },
            (err) => {
                // console.log('所有响应错误拦截',err);
                return err
            }
        )
    }

    get(url) {
        return this.request(url, 'GET')
    }

    post(url, data, config) {
        return this.request(url, 'POST', data, config)
    }

    postForm(url, data) {
        return this.request(url, 'POST', data, {
            'Content-Type': 'application/x-www-form-urlencoded'
        })
    }

    request(url, method, data, headers) {
        return new Promise((resolve, reject) => {
            this.instance.request({
                baseURL: BASE_URL_SERVE,
                url: url,
                method: method,
                data: data,
                withCredentials: true,
                headers: headers,
            }).then(res => {
                resolve(res.data)//响应成功直接抽出res中的data,也就是后端返回的实体类FangResponse
            }).catch(err => {
                reject(err)
            })
        })

    }
}

export const faRequest = new FARequest()



export let ws
export const createWebsocket = (id) => {

    ws = new WebSocket(BASE_URL_WEBSOCKET_SERVE+"/"+id)
    ws.onopen = function (evt) {


    };


    // 3.服务器每次返回信息都会执行一次onmessage方法
    ws.onmessage = function (res) {
        // console.log("服务器返回的信息: " ,res);
        onWSMessage(res)

    };

    ws.onerror = function (evt) {
        //###console.log("Connection error.");
        ws.close()
    };

    ws.onclose = function (evt) {
        //###console.log("Connection closed.");

    };
    ws.sendMsg=function (data){
        ws.send(JSON.stringify(data))
    }
}





